﻿using FieldTools.Client.Command;
using FieldTools.Client.ViewModel.Window;
using FieldTools.Util;
using ICSharpCode.AvalonEdit.Folding;
using MahApps.Metro.Controls;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FieldTools.Client.View.Window
{
    /// <summary>
    /// XMLUserControl.xaml 的交互逻辑
    /// </summary>
    //[OpenOnlyOne]
    public partial class XMLWindow : MetroWindow, ITransientDependency
    {
        /// <summary>
        /// 是否已打开窗口（定义该静态属性后即表示当前窗口仅能打开一个）
        /// </summary>
        public static bool IsOpened { get; set; }
        public XMLWindow(XMLViewModel vm)
        {
            InitializeComponent();
            this.DataContext = vm;
            //折叠
            foldingManager = FoldingManager.Install(CodeEditor.TextArea);
            this.Closing += XMLWindow_Closing;
        }

        private void XMLWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
        {
            webView.Dispose();
        }

        FoldingManager foldingManager = null;
        XmlFoldingStrategy foldingStrategy = new XmlFoldingStrategy();

        private void CodeEditor_TextChanged(object sender, EventArgs e)
        {
            if (foldingManager == null) return;
            foldingStrategy.UpdateFoldings(foldingManager, CodeEditor.Document);
        }
        private void CloseMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (foldingManager == null) return;
            var isFrist = true;
            foreach (var item in foldingManager.AllFoldings)
            {
                if (isFrist)
                {
                    isFrist = false;
                    continue;
                }
                item.IsFolded = true;
            }
        }
        private void OpenMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (foldingManager == null) return;
            foreach (var item in foldingManager.AllFoldings)
            {
                item.IsFolded = false;
            }
        }
    }
}
